package com.shermanzhou.multithread;


import java.util.ArrayList;
import java.util.List;

/**
 * @author zhouchaoqun02
 * @date 2019-10-09 19:59
 */
public class ListDivideDemo {

    public static <T> List<List<T>> averageAssign(List<T> source, int n) {
        List<List<T>> result = new ArrayList<>();
        int remaider = source.size() % n;  //(先计算出余数)
        int number = source.size() / n;  //然后是商
        int offset = 0;//偏移量
        for (int i = 0; i < n; i++) {
            List<T> value = null;
            if (remaider > 0) {
                value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                remaider--;
                offset++;
            } else {
                value = source.subList(i * number + offset, (i + 1) * number + offset);
            }
            result.add(value);
        }
        return result;
    }

    public static void main(String[] args) {

        List<String> list = new ArrayList<>();
        for (int i = 0; i < 113; i++) {
            list.add(String.valueOf(i));
        }
        List<List<String>> lists = averageAssign(list, 20);
        System.out.println(lists.size());
        System.out.println(lists);

//        int part = 12;
//        int perCount = list.size() / part;
//
//        int begin = 0;
//        int end = perCount == 0 ? list.size() : perCount;
//
//        int partIndex = 1;
//        do {
//            System.out.println("begin:"+begin);
//            System.out.println("end:"+end);
//            List<String> sub = list.subList(begin, end);
//            System.out.println("part " +partIndex+ " : "+sub);
//            partIndex++;
//
//            begin = end;
//            end = end + perCount;
//            if (end > list.size()) {
//                end = list.size();
//            }
//        } while (begin < list.size());
    }
}
